#include "utility.h"
#include <matrix.h>
#include <mat.h>
#include <memory.h>



view_s utility::loadViewData(const char *img_name){
	view_s view; /*to be returned object*/
	
	char mat_file_name[500];
	sprintf(&mat_file_name[0],"%s.view.mat",img_name);
	MATFile *p_file= matOpen(mat_file_name,"r");
	if(p_file == NULL){
		printf("Error, can not open file %s \n", img_name);
		return view;
	}

	mxArray *p_mat;
	float *p_float;

	p_mat=matGetVariable(p_file,"cam_dir");
	memcpy(&(view.cam_dir[0]),(void*)mxGetPr(p_mat),sizeof(float)*3);
	mxDestroyArray(p_mat);

	p_mat=matGetVariable(p_file,"P");/*assume P is columnwise matrix or vector*/
	p_float = (float*)mxGetPr(p_mat);
	for(int row =0; row<3; ++row){
		for(int col =0; col<4; ++col){
			view.P[row][col]=p_float[row + col*3];
		}
	}
	mxDestroyArray(p_mat);

	p_mat = matGetVariable(p_file,"zx");
	view.zx = *mxGetPr(p_mat);
	mxDestroyArray(p_mat);

	p_mat = matGetVariable(p_file,"zy");
	view.zy = *mxGetPr(p_mat);
	mxDestroyArray(p_mat);

	matClose(p_file);
	return view;
}